package adventure.placeable.trigger.results;

import colour.ColourUtil;
import java.util.StringTokenizer;

import player.Player;
import player.PlayerEdit;
import adventure.room.RoomManager;
import server.util.Broadcaster;

/**
 *
 * @author Michael Hanns
 *
 */
public class ResultMovePlayer extends TriggerResult implements Cloneable {

    private String backupResultText;
    private boolean resultTextChanged;
    private int dx;
    private int dy;
    private int dz;

    public ResultMovePlayer(String result, int uses, int x, int y, int z) {
        super(result, uses);
        this.dx = x;
        this.dy = y;
        this.dz = z;
        this.backupResultText = result;
        this.resultTextChanged = false;
    }

    public ResultMovePlayer(String result, int x, int y, int z) {
        super(result);
        this.dx = x;
        this.dy = y;
        this.dz = z;
        this.backupResultText = result;
        this.resultTextChanged = false;
    }

    public ResultMovePlayer() {
        super();
        this.backupResultText = super.getResultText();
        this.dx = 1;
        this.dy = 1;
        this.dz = 1;
        this.resultTextChanged = false;
    }

    @Override
    public void setResultText(String result) {
        this.resultText = result;
        this.backupResultText = "";
    }

    public void setDestX(int x) {
        this.dx = x;
    }

    public void setDestY(int y) {
        this.dy = y;
    }

    public void setDestZ(int z) {
        this.dz = z;
    }

    public int x() {
        return dx;
    }

    public int y() {
        return dy;
    }

    public int z() {
        return dz;
    }

    @Override
    public String getDescription() {
        if (super.limitedUses()) {
            String desc = super.resultText + ", uses: " + super.usesRemaining()
                    + " / MOVEPLAYER (" + dx + ", " + dy + ", " + dz + ")";
            return desc;
        } else {
            String desc = super.resultText
                    + " / MOVEPLAYER (" + dx + ", " + dy + ", " + dz + ")";
            return desc;
        }
    }

    @Override
    public String resultImplementation(RoomManager r, Player p, String in, boolean previousFailed, Broadcaster out) {
        if (allConditionsMet(r, p, in, previousFailed)) {
            out.broadcastLocalToPlayer(p, ColourUtil.colourise(p.getAliveName(), ColourUtil.player) + " leaves.");
            executeMove(p, r);
            useOnce();
            out.broadcastLocalToPlayer(p, ColourUtil.colourise(p.getAliveName(), ColourUtil.player) + " enters.");
            String output = replaceKeywords(replaceKeywords(p), r, p.x(), p.y(), p.z())
                    + "\n\n" + printRoom(r, p);

            return replaceYou(output, p, false);
        } else {
            return "";
        }
    }

    @Override
    public String getType() {
        return "MOVEPLAYER";
    }

    private void executeMove(Player p, RoomManager r) {
        r.leaveRoom(p);
        ((PlayerEdit) p).setX(dx);
        ((PlayerEdit) p).setY(dy);
        ((PlayerEdit) p).setZ(dz);
        r.enterRoom(p);
    }

    @Override
    public String replaceKeywords(Player p) {
        if (resultTextChanged) {
            resultText = backupResultText;
            resultTextChanged = false;
        }
        StringTokenizer tokens = new StringTokenizer(resultText);
        String finalText = "";
        while (tokens.hasMoreTokens()) {
            String word = tokens.nextToken();
            if (word.equalsIgnoreCase("[PLAYER]")) {
                word = p.getName();
            } else if (word.equalsIgnoreCase("[USES]")) {
                word = super.usesRemaining() + "";
            }

            if (finalText.length() > 0) {
                finalText = finalText + " " + word;
            } else {
                finalText = word;
            }
        }
        return finalText;
    }

    private String printRoom(RoomManager rooms, Player activePlayer) {
        int x = activePlayer.x();
        int y = activePlayer.y();
        int z = activePlayer.z();

        String output = ColourUtil.colourise(rooms.getRoomTitle(x, y, z), ColourUtil.roomTitle);
        if (activePlayer.isAdmin()) {
            output += " (" + x + ", " + y + ", " + z + ")";
        }
        output += ColourUtil.colourise("\n------------\n", ColourUtil.white);
        output += ColourUtil.colourise(rooms.getRoomDescription(x, y, z), ColourUtil.roomDescription) + "\n\n";
        output += rooms.getRoomYouSee(x, y, z, activePlayer.getID()) + "\n";
        output += ColourUtil.colourise("Available paths:\n", ColourUtil.white)
                + ColourUtil.colourise(rooms.getPaths(x, y, z), ColourUtil.grey);

        return output;
    }

    
    private String replaceKeywords(String text, RoomManager r, int x, int y, int z) {
       return text
               .replace("[TITLE]", ColourUtil.colourise(r.getRoomTitle(x, y, z), ColourUtil.roomTitle))
               .replace("[DESCRIPTION]", ColourUtil.colourise(r.getRoomDescription(x, y, z), ColourUtil.roomDescription))
               .replace("[USES]", ColourUtil.colourise(super.usesRemaining() + "", ColourUtil.value));

    }
}
